package user_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/models/ctype"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
	"time"
)

type UserInfoResponse struct {
	UserID         uint                 `json:"userID"`
	Nickname       string               `json:"nickname"`
	Role           ctype.Role           `json:"role"`
	CreatedAt      time.Time            `json:"createdAt"`
	Avatar         string               `json:"avatar"`
	Email          string               `json:"email"`
	Scope          int                  `json:"scope"`
	RegisterSource ctype.RegisterSource `json:"registerSource"`
	IsPwdBind      bool                 `json:"isPwdBind"`
}

func (u UserApi) UserInfoView(c *gin.Context) {

	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)

	var user models.UserModel
	err := global.DB.Take(&user, claims.UserID).Error
	if err != nil {
		res.FailWithMsg(c, "用户不存在")
		return
	}
	info := UserInfoResponse{
		UserID:         user.ID,
		Nickname:       user.Nickname,
		Role:           user.Role,
		CreatedAt:      user.CreatedAt,
		Avatar:         user.Avatar,
		Email:          user.Email,
		Scope:          user.Scope,
		RegisterSource: user.RegisterSource,
	}
	if user.Password != "" {
		info.IsPwdBind = true
	}
	res.OkWithData(c, info)
}
